home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / lisp.el < prev    next >
Encoding:
Text File  |  1995-07-19  |  9.9 KB  |  296 lines

  1. ;;; lisp.el --- Lisp editing commands for XEmacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: lisp, languages
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; Lisp editing commands to go with Lisp major mode.
  27.  
  28. ;;; Code:
  29.  
  30. ;; Note that this variable is used by non-lisp modes too.
  31. (defvar defun-prompt-regexp nil
  32.   "*Non-nil => regexp to ignore, before the character that starts a defun.
  33. This is only necessary if the opening paren or brace is not in column 0.
  34. See `beginning-of-defun'.")
  35. (make-variable-buffer-local 'defun-prompt-regexp)
  36.  
  37. (defvar parens-require-spaces t
  38.   "Non-nil => `insert-parentheses' should insert whitespace as needed.")
  39.  
  40. (defun forward-sexp (&optional arg)
  41.   "Move forward across one balanced expression (sexp).
  42. With argument, do it that many times.
  43. Negative arg -N means move backward across N balanced expressions."
  44.   (interactive "_p")
  45.   (or arg (setq arg 1))
  46.   (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
  47.   (if (< arg 0) (backward-prefix-chars)))
  48.  
  49. (defun backward-sexp (&optional arg)
  50.   "Move backward across one balanced expression (sexp).
  51. With argument, do it that many times.
  52. Negative arg -N means move forward across N balanced expressions."
  53.   (interactive "_p")
  54.   (or arg (setq arg 1))
  55.   (forward-sexp (- arg)))
  56.  
  57. (defun mark-sexp (arg)
  58.   "Set mark ARG sexps from point.
  59. The place mark goes is the same place \\[forward-sexp] would move to
  60. with the same argument.
  61. Repeat this command to mark more sexps in the same direction."
  62.   (interactive "p")
  63.   (mark-something 'mark-sexp 'forward-sexp arg))
  64.  
  65. (defun forward-list (&optional arg)
  66.   "Move forward across one balanced group of parentheses.
  67. With argument, do it that many times.
  68. Negative arg -N means move backward across N groups of parentheses."
  69.   (interactive "_p")
  70.   (or arg (setq arg 1))
  71.   (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
  72.  
  73. (defun backward-list (&optional arg)
  74.   "Move backward across one balanced group of parentheses.
  75. With argument, do it that many times.
  76. Negative arg -N means move forward across N groups of parentheses."
  77.   (interactive "_p")
  78.   (or arg (setq arg 1))
  79.   (forward-list (- arg)))
  80.  
  81. (defun down-list (arg)
  82.   "Move forward down one level of parentheses.
  83. With argument, do this that many times.
  84. A negative argument means move backward but still go down a level.
  85. In Lisp programs, an argument is required."
  86.   (interactive "_p")
  87.   (let ((inc (if (> arg 0) 1 -1)))
  88.     (while (/= arg 0)
  89.       (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
  90.       (setq arg (- arg inc)))))
  91.  
  92. (defun backward-up-list (arg)
  93.   "Move backward out of one level of parentheses.
  94. With argument, do this that many times.
  95. A negative argument means move forward but still to a less deep spot.
  96. In Lisp programs, an argument is required."
  97.   (interactive "p")
  98.   (up-list (- arg)))
  99.  
  100. (defun up-list (arg) 
  101.   "Move forward out of one level of parentheses.
  102. With argument, do this that many times.
  103. A negative argument means move backward but still to a less deep spot.
  104. In Lisp programs, an argument is required."
  105.   (interactive "_p")
  106.   (let ((inc (if (> arg 0) 1 -1)))
  107.     (while (/= arg 0)
  108.       (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
  109.       (setq arg (- arg inc)))))
  110.  
  111. (defun kill-sexp (arg)
  112.   "Kill the sexp (balanced expression) following the cursor.
  113. With argument, kill that many sexps after the cursor.
  114. Negative arg -N means kill N sexps before the cursor."
  115.   (interactive "p")
  116.   (let ((opoint (point)))
  117.     (forward-sexp arg)
  118.     (kill-region opoint (point))))
  119.  
  120. (defun backward-kill-sexp (arg)
  121.   "Kill the sexp (balanced expression) preceding the cursor.
  122. With argument, kill that many sexps before the cursor.
  123. Negative arg -N means kill N sexps after the cursor."
  124.   (interactive "p")
  125.   (kill-sexp (- arg)))
  126.  
  127. (defun beginning-of-defun (&optional arg)
  128.   "Move backward to the beginning of a defun.
  129. With argument, do it that many times.  Negative arg -N
  130. means move forward to Nth following beginning of defun.
  131. Returns t unless search stops due to beginning or end of buffer.
  132.  
  133. Normally a defun starts when there is an char with open-parenthesis
  134. syntax at the beginning of a line.  If `defun-prompt-regexp' is
  135. non-nil, then a string which matches that regexp may precede the
  136. open-parenthesis, and point ends up at the beginning of the line."
  137.   (interactive "_p")
  138.   (and (beginning-of-defun-raw arg)
  139.        (progn (beginning-of-line) t)))
  140.  
  141. (defun beginning-of-defun-raw (&optional arg)
  142.   "Move point to the character that starts a defun.
  143. This is identical to beginning-of-defun, except that point does not move
  144. to the beginning of the line when `defun-prompt-regexp' is non-nil."
  145.   (interactive "p")
  146.   (and arg (< arg 0) (not (eobp)) (forward-char 1))
  147.   (and (re-search-backward (if defun-prompt-regexp
  148.                    (concat "^\\s(\\|"
  149.                        "\\(" defun-prompt-regexp "\\)\\s(")
  150.                  "^\\s(")
  151.                nil 'move (or arg 1))
  152.        (progn (goto-char (1- (match-end 0)))) t))
  153.  
  154. (defun buffer-end (arg)
  155.   (if (> arg 0) (point-max) (point-min)))
  156.  
  157. (defun end-of-defun (&optional arg)
  158.   "Move forward to next end of defun.  With argument, do it that many times.
  159. Negative argument -N means move back to Nth preceding end of defun.
  160.  
  161. An end of a defun occurs right after the close-parenthesis that matches
  162. the open-parenthesis that starts a defun; see `beginning-of-defun'."
  163.   (interactive "_p")
  164.   (if (or (null arg) (= arg 0)) (setq arg 1))
  165.   (let ((first t))
  166.     (while (and (> arg 0) (< (point) (point-max)))
  167.       (let ((pos (point)))
  168.     (while (progn
  169.         (if (and first
  170.              (progn
  171.               (end-of-line 1)
  172.               (beginning-of-defun-raw 1)))
  173.             nil
  174.           (or (bobp) (forward-char -1))
  175.           (beginning-of-defun-raw -1))
  176.         (setq first nil)
  177.         (forward-list 1)
  178.         (skip-chars-forward " \t")
  179.         (if (looking-at "\\s<\\|\n")
  180.             (forward-line 1))
  181.         (<= (point) pos))))
  182.       (setq arg (1- arg)))
  183.     (while (< arg 0)
  184.       (let ((pos (point)))
  185.     (beginning-of-defun-raw 1)
  186.     (forward-sexp 1)
  187.     (forward-line 1)
  188.     (if (>= (point) pos)
  189.         (if (beginning-of-defun-raw 2)
  190.         (progn
  191.           (forward-list 1)
  192.           (skip-chars-forward " \t")
  193.           (if (looking-at "\\s<\\|\n")
  194.               (forward-line 1)))
  195.           (goto-char (point-min)))))
  196.       (setq arg (1+ arg)))))
  197.  
  198. (defun mark-defun ()
  199.   "Put mark at end of this defun, point at beginning.
  200. The defun marked is the one that contains point or follows point."
  201.   (interactive)
  202.   (push-mark (point))
  203.   (end-of-defun)
  204.   (push-mark (point) nil t)
  205.   (beginning-of-defun)
  206.   (re-search-backward "^\n" (- (point) 1) t))
  207.  
  208. (defun insert-parentheses (arg)
  209.   "Put parentheses around next ARG sexps.  Leave point after open-paren.
  210. No argument is equivalent to zero: just insert `()' and leave point between.
  211. If `parens-require-spaces' is non-nil, this command also inserts a space
  212. before and after, depending on the surrounding characters."
  213.   (interactive "P")
  214.   (if arg (setq arg (prefix-numeric-value arg))
  215.     (setq arg 0))
  216.   (or (eq arg 0) (skip-chars-forward " \t"))
  217.   (and parens-require-spaces
  218.        (not (bobp))
  219.        (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
  220.        (insert " "))
  221.   (insert ?\()
  222.   (save-excursion
  223.     (or (eq arg 0) (forward-sexp arg))
  224.     (insert ?\))
  225.     (and parens-require-spaces
  226.      (not (eobp))
  227.      (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
  228.      (insert " "))))
  229.  
  230. (defun move-past-close-and-reindent ()
  231.   "Move past next `)', delete indentation before it, then indent after it."
  232.   (interactive)
  233.   (up-list 1)
  234.   (forward-char -1)
  235.   (while (save-excursion        ; this is my contribution
  236.        (let ((before-paren (point)))
  237.          (back-to-indentation)
  238.          (= (point) before-paren)))
  239.     (delete-indentation))
  240.   (forward-char 1)
  241.   (newline-and-indent))
  242.  
  243. (defun lisp-complete-symbol ()
  244.   "Perform completion on Lisp symbol preceding point.
  245. That symbol is compared against the symbols that exist
  246. and any additional characters determined by what is there
  247. are inserted.
  248. If the symbol starts just after an open-parenthesis,
  249. only symbols with function definitions are considered.
  250. Otherwise, all symbols with function definitions, values
  251. or properties are considered."
  252.   (interactive)
  253.   (let* ((end (point))
  254.      (buffer-syntax (syntax-table))
  255.      (beg (unwind-protect
  256.           (save-excursion
  257.             (if emacs-lisp-mode-syntax-table
  258.             (set-syntax-table emacs-lisp-mode-syntax-table))
  259.             (backward-sexp 1)
  260.             (while (= (char-syntax (following-char)) ?\')
  261.               (forward-char 1))
  262.             (point))
  263.         (set-syntax-table buffer-syntax)))
  264.      (pattern (buffer-substring beg end))
  265.      (predicate
  266.       (if (eq (char-after (1- beg)) ?\()
  267.           'fboundp
  268.         #'(lambda (sym)
  269.         (or (boundp sym) (fboundp sym)
  270.             (symbol-plist sym)))))
  271.      (completion (try-completion pattern obarray predicate)))
  272.     (cond ((eq completion t))
  273.       ((null completion)
  274.        (message "Can't find completion for \"%s\"" pattern)
  275.        (ding))
  276.       ((not (string= pattern completion))
  277.        (delete-region beg end)
  278.        (insert completion))
  279.       (t
  280.        (message "Making completion list...")
  281.        (let ((list (all-completions pattern obarray predicate)))
  282.          (or (eq predicate 'fboundp)
  283.          (let (new)
  284.            (while list
  285.              (setq new (cons (if (fboundp (intern (car list)))
  286.                      (list (car list) " <f>")
  287.                        (car list))
  288.                      new))
  289.              (setq list (cdr list)))
  290.            (setq list (nreverse new))))
  291.          (with-output-to-temp-buffer "*Completions*"
  292.            (display-completion-list list)))
  293.        (message "Making completion list...done")))))
  294.  
  295. ;;; lisp.el ends here
  296.